<?php
/**
 */
class ExpressionsSentTable extends Doctrine_Table
{
	public function setstatusInactive($rec_id,$sender_id)
	{			    
	        $query = new Doctrine_Query();
		$query->update('ExpressionsSent e')
			  ->set('e.status','"Inactive"')
			  ->where("e.sender_id='".$sender_id."'")
			  ->andWhere("e.receiver_id='".$rec_id."'");
			  //echo $query;exit;
		return $query->execute();	
	}
	public function deleteExpression($rec_id,$status,$expr_id)
	{
		$query = new Doctrine_Query();
		$query->update('ExpressionsSent e')
			  ->set('e.status',"'$status'")
			  ->where("e.expression_id='".$expr_id."'")
			  ->andWhere("e.receiver_id='".$rec_id."'");
			  //echo $query->getSql();exit;
		$result=$query->execute();
	return $result;		
	}
	public function updateExpressionReadStatus($user_id,$expr_id,$status)
	{
		$query = new Doctrine_Query();
		$query->update('ExpressionsSent e')
			  ->set('e.read_status',"'$status'")
			  ->where("e.expression_id='".$expr_id."'")
			  ->andWhere("e.receiver_id='".$user_id."'");
			   //echo $query;exit;
		$result=$query->execute();
	return $result;		
	}
	public function deleteSentExpression($sender_id,$status,$expr_id)
	{
		$query = new Doctrine_Query();
		$query->update('ExpressionsSent e')
			  ->set('e.status',"'$status'")
			  ->where("e.expression_id='".$expr_id."'")
			  ->andWhere("e.sender_id='".$sender_id."'");
			  //echo $query;exit;
			$result=$query->execute();
			return $result;	
	}
	public function getBlockedExpressions($userId,$currentPage,$resultsPerPage)
	{
		$query = new Doctrine_Query();
		$query->select('e.*,u.photo as photo,m.message_intensity as message_intensity,m.desc as desc,i.first_name as first_name,i.user_id as user_id')
		       ->from('ExpressionsSent e')
			   ->leftJoin('e.Expressions x ON e.sender_id=u.user_id')
			   ->leftJoin('e.MessageIntensity m ON m.id=e.message_color_id')
			   ->leftJoin('e.Users i ON i.user_id=e.sender_id')
			   ->where("e.email='".$userId."'")
			   ->andWhere("e.status !='RDeleted'")
			   //->andWhere("e.anonymous='0'")
			   //->andWhere("e.status !='Deleted'")
			   ->andWhere("e.status !='Inactive'")
			   ->groupBy('e.id')
			   ->orderBy("e.id desc");
				//echo $query->getSql();exit;
			require_once('../database/models/Pager.php');
			$pager = new Pager($currentPage,$resultsPerPage);
			//echo $query->getSql();
			$result = $pager->pagination($query);
			//echo '<pre>';print_r($result);exit;
			return $result;
	        
	}
	public function addExpressions($email,$expr_id,$receiver_id,$is_group,$memid)
	{
		
		$ExpressionsSent = new ExpressionsSent();
		$ExpressionsSent ->expression_id = $expr_id;
		$ExpressionsSent ->sender_id = $memid;
		$ExpressionsSent ->receiver_email = $email;
		$ExpressionsSent ->receiver_id = $receiver_id;
		$ExpressionsSent ->is_group = $is_group ;
		$ExpressionsSent ->status = "active";
		$ExpressionsSent ->read_status = 0;
		//echo '<pre>';print_r($ExpressionsSent);exit;
		$ExpressionsSent ->save();
		return $ExpressionsSent->id;
	}
	public function addGroupExpressions($expr_id,$group_id,$is_group,$memid)
	{
		
		$ExpressionsSent = new ExpressionsSent();
		$ExpressionsSent ->expression_id = $expr_id;
		$ExpressionsSent ->sender_id = $memid;
		$ExpressionsSent ->receiver_email = "";
		$ExpressionsSent ->receiver_id = $group_id;
		$ExpressionsSent ->is_group = $is_group ;
		$ExpressionsSent ->status = "active";
		$ExpressionsSent ->read_status = 0;
		//echo '<pre>';print_r($ExpressionsSent);exit;
		$ExpressionsSent ->save();
		return $ExpressionsSent->id;
	}
	public function getReceivedMails($mailid)
	{
			$query=new Doctrine_Query();
		    $query->select('count(e.receiver_email) as numCount')
				  ->from('ExpressionsSent e')
			      ->leftJoin('e.Expressions u')
			      ->where("e.receiver_email='".$mailid."'")
			      ->andWhere("e.status!='RDeleted'")
				  ->andWhere("e.status!='Delete'")
				  ->andWhere("e.status!='Inactive'")
			      ->andWhere("u.send_type='1'");
			   //->andWhere("e.status!='Deleted'");
			  // ->andWhere("e.anonymous='0'");
			   //echo $query;exit;
			   	$result = $query->execute();
				return $result[0];
				
		
	}
	 public function getMails($memid)
	 {
		$query=new Doctrine_Query();
		    $query->select('DISTINCT(s.receiver_email) as email')
		       ->from('ExpressionsSent s')
			   ->where("s.sender_id='".$memid."'");
			     //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	 }
	public function deleteAnonymous($status,$expr_id)
	{
		$query = new Doctrine_Query();
		$query->update('ExpressionsSent')
			  ->set('status',"'$status'")
			  ->where("expression_id IN (".$expr_id.")");
			  //echo $query;exit;
		return $query->execute();
	}
	public function deleteExpressionsByExpressionId($expr_id)
	{
		$query = new Doctrine_Query();
		$query->delete ('expression_id')
			  ->from('ExpressionsSent s')
			  ->where("expression_id IN (".$expr_id.")");
			  //echo $query->getSql1Query();exit;
		return $query->execute();
	}
}